// Librairies LUA
extern "C" {
	#include <lauxlib.h>
	#include <lualib.h>
}
#include <stdlib.h>
#include <stdio.h>
#include <string>

using namespace std;

int main(void)
{
    lua_State *L;
    string text = "Hello world\n";
    int nbLines = 5;

    /*
     * Instance de l'interface LUA
     */
    L = luaL_newstate();
    luaL_openlibs(L); /* Chargement des libraries standard */
    if ( luaL_dofile(L, "configuration.lua") ) {  /* Chargement du fichier */
        fprintf(stderr, "Impossible de charger le fichier de configuration: %s\n", lua_tostring(L, -1));
        exit(1);
    }

	// recupere la variable text
    lua_settop(L,0);
	lua_getfield(L, LUA_GLOBALSINDEX, "TEXT");
	if (lua_isstring(L, -1)) {
		text = lua_tostring(L, -1);
	}
	lua_pop(L, 1);

	// recupere la variable nbLines
    lua_settop(L,0);
	lua_getfield(L, LUA_GLOBALSINDEX, "NB_LINES");
	if (lua_isnumber(L, -1)) {
		nbLines = lua_tonumber(L, -1);
	}
	lua_pop(L, 1);

	for (int i=0; i<nbLines; i++)
		printf(text.c_str());

    return 0;
}
